package com.example.mymvvmdemo.models.device_detail;

import android.app.Activity;
import android.content.Intent;
import android.view.View;

import androidx.lifecycle.ViewModelProvider;

import com.example.mymvvmdemo.R;
import com.example.mymvvmdemo.base.BaseLoadActivity;
import com.example.mymvvmdemo.constants.Constants;
import com.example.mymvvmdemo.constants.UrlConstants;
import com.example.mymvvmdemo.constants.UrlTypeConstants;
import com.example.mymvvmdemo.databinding.ActivityDeviceDetailBinding;
import com.example.mymvvmdemo.dialog.LoadingAnimDialog;
import com.example.mymvvmdemo.dialog.PromptDialog;
import com.example.mymvvmdemo.entity.FailEntity;
import com.example.mymvvmdemo.models.add_device.entity.DeviceTypeListEntity;
import com.example.mymvvmdemo.models.bind_email.entity.BindAccountEntity;
import com.example.mymvvmdemo.models.device_detail.entity.DeviceDetailEntity;
import com.example.mymvvmdemo.models.main.fragment.device.entity.DeviceListEntity;
import com.example.mymvvmdemo.net.presenter.DeletePresenter;
import com.example.mymvvmdemo.net.presenter.GetPresenter;
import com.example.mymvvmdemo.net.view.IView;
import com.example.mymvvmdemo.utils.GlideUtils;
import com.example.mymvvmdemo.utils.StrUtils;
import com.example.mymvvmdemo.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;

public class DeviceDetailActivity extends BaseLoadActivity<ActivityDeviceDetailBinding> implements View.OnClickListener, IView.GetView, IView.DeleteView {

    public static void startDeviceDetailActivity(Activity activity, DeviceListEntity.DataDTO.DevicesDTO devicesDTO) {
        Intent intent = new Intent(activity, DeviceDetailActivity.class);
        intent.putExtra("device", devicesDTO);
        activity.startActivity(intent);
    }

    public DeviceDetailViewModel viewModel;
    private GetPresenter getPresenter;
    private DeletePresenter deletePresenter;

    private PromptDialog deleteDialog;

    /*** 加载时候的Dialog */
    private LoadingAnimDialog loadingDialog;


    @Override
    protected void showDialog() {
        if (loadingDialog == null)
            loadingDialog = new LoadingAnimDialog(this, getResources().getString(R.string.load));
        if (!loadingDialog.isShowing()) loadingDialog.show(this);
    }

    @Override
    protected void dismissDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.close(this);
        }
    }

    @Override
    protected void initData() {
        viewModel = new ViewModelProvider(this).get(DeviceDetailViewModel.class);
        viewModel.devices = (DeviceListEntity.DataDTO.DevicesDTO) getIntent().getSerializableExtra("device");
        getPresenter = new GetPresenter(this);
        deletePresenter = new DeletePresenter(this);
        initTitle(viewModel.devices.getName());
        startLoadDeviceDetail();

    }

    @Override
    public void initDialog() {
        super.initDialog();
        deleteDialog = new PromptDialog(this);
        deleteDialog.setText(R.string.cancel, R.string.confirm);
        deleteDialog.setText(R.string.device_detail_delete_title);
        deleteDialog.setListener(new PromptDialog.PromptClickListener() {
            @Override
            public void onConfirmClick() {
                startLoadDeviceDelete();
            }
        });
    }

    @Override
    protected void initListener() {
        binding.include.ivBack.setOnClickListener(this);
        binding.tvDelete.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_delete:
                if (deleteDialog != null) {
                    deleteDialog.show();
                }
                break;
            default:
                break;
        }
    }

    /***********************************自定义方法**************************************/
    public void initTitle(String title) {
        binding.include.tvTitle.setVisibility(View.VISIBLE);
        binding.include.tvTitle.setText(title);
    }

    public void startLoadDeviceDetail() {
        showDialog();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("id", "" + viewModel.devices.getId());
        getPresenter.startLoadGet(UrlConstants.deviceDetail, hashMap, UrlTypeConstants.deviceDetail);
    }

    public void startLoadDeviceDelete() {
        showDialog();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("id", "" + viewModel.devices.getId());
        deletePresenter.startLoadDelete(UrlConstants.deviceDelete, hashMap, UrlTypeConstants.deviceDelete);
    }

    private void startLoadDeviceType() {
        showDialog();
        HashMap<String, String> hashMap = new HashMap<>();
        getPresenter.startLoadGet(UrlConstants.deviceTypeList, hashMap, UrlTypeConstants.deviceTypeList);
    }

    private void notifytionImg() {
        if (viewModel.deviceType.equals("petcollat")) {//宠物项圈
            GlideUtils.loadImage(this, R.drawable.ic_svg_cwxq, R.mipmap.app_icon, binding.ivHead);
        }
        if (viewModel.deviceType.equals("timer")) {//计时器
            GlideUtils.loadImage(this, R.drawable.ic_svg_jsq, R.mipmap.app_icon, binding.ivHead);
        }
        if (viewModel.deviceType.equals("audio")) {//音频对话
            GlideUtils.loadImage(this, R.drawable.ic_svg_ypdh, R.mipmap.app_icon, binding.ivHead);
        }
        if (viewModel.deviceType.equals("sensor")) {//传感器
            GlideUtils.loadImage(this, R.drawable.ic_svg_cgq, R.mipmap.app_icon, binding.ivHead);
        }
        if (viewModel.deviceType.equals("controller")) {//控制器
            GlideUtils.loadImage(this, R.drawable.ic_svg_kzq, R.mipmap.app_icon, binding.ivHead);
        }
        if (viewModel.deviceType.equals("camera")) {//摄像头
            GlideUtils.loadImage(this, R.drawable.ic_svg_sxt, R.mipmap.app_icon, binding.ivHead);
        }
        if (viewModel.deviceType.equals("display")) {//显示屏
            GlideUtils.loadImage(this, R.drawable.ic_svg_xsq, R.mipmap.app_icon, binding.ivHead);
        }
        if (viewModel.deviceType.equals("gateway")) {//网关
            GlideUtils.loadImage(this, R.drawable.ic_svg_wg, R.mipmap.app_icon, binding.ivHead);
        }
        if (viewModel.deviceType.equals("other")) {//其他
            GlideUtils.loadImage(this, R.drawable.ic_svg_else, R.mipmap.app_icon, binding.ivHead);
        }
    }

    /***********************************数据请求回调**************************************/
    @Override
    public void loadGetSuccess(String result, String type, int code) {
        if (UrlTypeConstants.deviceDetail.equals(type)) {
            loadDeviceDetailSuccess(result, code);
        }
        if (type.equals(UrlTypeConstants.deviceTypeList)) {
            loadDeviceTypeListSuccess(result, code);
        }
    }

    @Override
    public void loadGetFail(String message, String type) {
        if (UrlTypeConstants.deviceDetail.equals(type)) {
            loadDeviceDetailFail();
        }
        if (type.equals(UrlTypeConstants.deviceTypeList)) {
            loadDeviceTypeListFail();
        }
    }

    @Override
    public void loadGetAccountError(int code) {

    }

    @Override
    public void loadGetNetWorkError() {
        dismissDialog();
        toast(R.string.network_error, ToastUtils.TOA_W);
    }

    @Override
    public void loadDeleteSuccess(String result, String type, int code) {
        if (UrlTypeConstants.deviceDelete.equals(type)) {
            loadDeviceDeleteSuccess(result, code);
        }
    }

    @Override
    public void loadDeleteFail(String message, String type) {
        if (UrlTypeConstants.deviceDelete.equals(type)) {
            loadDeviceDeleteFail();
        }
    }

    @Override
    public void loadDeleteAccountError(int code) {

    }

    @Override
    public void loadDeleteNetWorkError(String message) {
        dismissDialog();
        toast(R.string.network_error, ToastUtils.TOA_W);
    }

    /***********************************处理请求后数据**************************************/
    private void loadDeviceTypeListSuccess(String result, int code) {
        if (code != 200) {
            loadDeviceTypeListFail();
            return;
        }
        dismissDialog();
        DeviceTypeListEntity deviceListEntity = gson.fromJson(result, DeviceTypeListEntity.class);
        DeviceTypeListEntity.DataDTO data = deviceListEntity.getData();
        if (data == null) {
            loadDeviceTypeListFail();
            return;
        }
        ArrayList<DeviceTypeListEntity.DataDTO.DeviceTypesDTO> deviceTypes = data.getDevice_types();
        if (deviceTypes == null) {
            loadDeviceTypeListFail();
            return;
        }
        for (int i = 0; i < deviceTypes.size(); i++) {
            DeviceTypeListEntity.DataDTO.DeviceTypesDTO deviceTypesDTO = deviceTypes.get(i);
            String code1 = deviceTypesDTO.getCode();
            if (code1.equals(viewModel.deviceType)) {
                viewModel.deviceType = deviceTypesDTO.getName();
                break;
            }
        }

        if (!StrUtils.isEmpty(viewModel.deviceType)) {
            binding.tvType.setText(viewModel.deviceType);
        }

    }

    private void loadDeviceTypeListFail() {
        dismissDialog();
        if (!StrUtils.isEmpty(viewModel.deviceType)) {
            binding.tvType.setText(viewModel.deviceType);
        }
    }

    private void loadDeviceDetailSuccess(String result, int code) {
        dismissDialog();
        if (code != 200) {
            FailEntity registerFailEntity = gson.fromJson(result, FailEntity.class);
            String message = registerFailEntity.getData().getMessage().getMessages().get(0);
            toast(message, ToastUtils.TOA_W);
            return;
        }

        try {
            DeviceDetailEntity detailEntity = gson.fromJson(result, DeviceDetailEntity.class);
            if (detailEntity == null) {
                return;
            }

            DeviceDetailEntity.DataDTO data = detailEntity.getData();
            if (data == null) {
                return;
            }

            DeviceDetailEntity.DataDTO.DeviceDTO device = data.getDevice();
            if (device == null) {
                return;
            }
            String name = device.getName();
            if (!StrUtils.isEmpty(name)) {
                binding.tvName.setText(name);
            }
            String status = device.getStatus();
            boolean isOffline = status.equals(Constants.OFFLINE);
            binding.circleView.setCircleColor(isOffline ? getColor(R.color.red_three) : getColor(R.color.green_one));

            String device_id = device.getDevice_id();
            if (!StrUtils.isEmpty(device_id)) {
                binding.tvId.setText(device_id);
            }
            String location = device.getLocation();
            if (!StrUtils.isEmpty(location)) {
                binding.tvLocation.setText(location);
            }
            String last_online_time = device.getLast_online_time();
            if (!StrUtils.isEmpty(last_online_time)) {
                binding.tvLast.setText(last_online_time);
            }
            String updated_at = device.getUpdated_at();
            if (!StrUtils.isEmpty(updated_at)) {
                binding.tvRegister.setText(updated_at);
            }

            String device_type = device.getDevice_type();
            viewModel.deviceType = device_type;
            notifytionImg();
            startLoadDeviceType();

        } catch (Exception e) {

        }

    }

    private void loadDeviceDetailFail() {
        dismissDialog();
    }

    private void loadDeviceDeleteSuccess(String result, int code) {
        dismissDialog();
        if (code != 200) {
            FailEntity registerFailEntity = gson.fromJson(result, FailEntity.class);
            String message = registerFailEntity.getData().getMessage().getMessages().get(0);
            toast(message, ToastUtils.TOA_W);
            return;
        }

        toast(R.string.success, ToastUtils.TOA_S);
        finish();

    }

    private void loadDeviceDeleteFail() {
        dismissDialog();
    }

}